#include "MyShellParser.h"
#include "MyShellException.h"

#include <iostream>
#include <string>
#include <queue>

// 要分析的单词序列
std::vector<mysh::Word> mysh::MyShellParser::words = std::vector<mysh::Word>();

// 所需分析的单词序列中单词的数量
std::size_t mysh::MyShellParser::word_num = 0;

// 当前分析的单词的索引
std::int32_t mysh::MyShellParser::cur_index = 0;

// 分析声明语句
std::shared_ptr<mysh::DeclareStatementNode> mysh::MyShellParser::parseDeclareStatement()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 获取数据类型
    mysh::WORD_TYPE data_type = mysh::WORD_TYPE::ILLEGAL_WORD;
    // 维度
    std::int32_t dimension = 0;
    // 每个维度的大小
    std::vector<std::size_t> size_of_every_demension = std::vector<std::size_t>();
    // 数组类型
    if (wrds[ci].word_type == mysh::WORD_TYPE::ARRAY)
    {
        // 至少有一维
        dimension++;
        // 获取数组内部类型
        ci++;
        // 当前应该是左尖括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LES)
        {
            // 尖括号计数
            std::int32_t angle_brackets_count = 0;
            angle_brackets_count++;
            ci++;
            while (ci < wn && angle_brackets_count > 0)
            {
                if (wrds[ci].word_type == mysh::WORD_TYPE::ARRAY)
                {
                    dimension++;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::LES)
                {
                    angle_brackets_count++;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::GTR)
                {
                    angle_brackets_count--;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::BOOL)
                {
                    data_type = mysh::WORD_TYPE::BOOL_VALUE;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::INT)
                {
                    data_type = mysh::WORD_TYPE::INTEGER;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::STRING)
                {
                    data_type = mysh::WORD_TYPE::STRING_VALUE;
                }
                else
                {
                    throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句缺少正确的数据类型关键字\033[0m");
                }
                ci++;
            }
            if (data_type == mysh::WORD_TYPE::ILLEGAL_WORD)
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句缺少正确的数据类型关键字\033[0m");
            }
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
            {
                ci++;
                while (ci < wn && wrds[ci].word_type != mysh::WORD_TYPE::RIGHT_PARENTHESES)
                {
                    if (wrds[ci].word_type == mysh::WORD_TYPE::INTEGER)
                    {
                        try
                        {
                            size_of_every_demension.push_back(std::stoi(wrds[ci].name));
                        }
                        catch(const std::exception& e)
                        {
                            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句指名各维度大小的数据有误: " + e.what() + "\033[0m");
                        }
                    }
                    else if (wrds[ci].word_type == mysh::WORD_TYPE::COMMA)
                    {
                        ci++;
                        continue;
                    }
                    else
                    {
                        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句指名各维度大小的单词类型有误\033[0m");
                    }
                    ci++;
                }
                if (size_of_every_demension.size() != dimension)
                {
                    throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句指名各维度大小的数据量与维度不匹配\033[0m");
                }
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句中的数组数据类型标记缺少指定各维度大小的小括号\033[0m");
            }
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句中的数组数据类型标记缺少尖括号\033[0m");
        }
    }
    else if (wrds[ci].word_type == mysh::WORD_TYPE::BOOL)
    {
        data_type = mysh::WORD_TYPE::BOOL_VALUE;
    }
    else if (wrds[ci].word_type == mysh::WORD_TYPE::INT)
    {
        data_type = mysh::WORD_TYPE::INTEGER;
    }
    else if (wrds[ci].word_type == mysh::WORD_TYPE::STRING)
    {
        data_type = mysh::WORD_TYPE::STRING_VALUE;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句缺少正确的数据类型关键字\033[0m");
    }
    ci++;
    std::vector<std::string> identifiers = std::vector<std::string>();
    while (ci < wn)
    {
        if (wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
        {
            identifiers.push_back(wrds[ci].name);
        }
        else if (wrds[ci].word_type == mysh::WORD_TYPE::COMMA)
        {
            ci++;
            continue;
        }
        else if (wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
        {
            ci++;
            break;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句数据类型关键字之后存在不合理的单词类型\033[0m");
        }
        ci++;
    }
    if (identifiers.size() == 0)
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m声明语句缺少标识符\033[0m");
    }
    return std::make_shared<mysh::DeclareStatementNode>(data_type, identifiers, dimension, size_of_every_demension);
}

// 分析运行语句
std::shared_ptr<mysh::RunStatementNode> mysh::MyShellParser::parseRunStatement()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // run之后需要有左小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句run之后缺少左小括号\033[0m");
    }
    // 左小括号之后为要运行的命令
    std::string command = std::string();
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::COMMAND)
    {
        command = wrds[ci].name;
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句run之后的小括号内缺少命令\033[0m");
    }
    //命令之后为右小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句指定的命令需要用小括号包起来\033[0m");
    }
    // 参数列表
    std::vector<mysh::Word> params = std::vector<mysh::Word>();
    // 参数列表被大括号括起来，但不一定有
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_BRACE)
    {
        ci++;
        while (ci < wn)
        {
            if (wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER || 
                wrds[ci].word_type == mysh::WORD_TYPE::INTEGER || 
                wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE || 
                wrds[ci].word_type == mysh::WORD_TYPE::BOOL_VALUE)
            {
                params.push_back(wrds[ci]);
                ci++;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句参数列表中存在不合理的单词类型\033[0m");
            }
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::COMMA)
            {
                ci++;
            }
            else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_BRACE)
            {
                ci++;
                break;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句参数列表请使用逗号进行分割\033[0m");
            }
        }
    }
    // 返回类型用冒号指定
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::COLON)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句缺少用于指定返回类型的冒号\033[0m");
    }
    // 获取返回数据类型
    mysh::WORD_TYPE data_type = mysh::WORD_TYPE::ILLEGAL_WORD;
    // 维度
    std::int32_t dimension = 0;
    // 每个维度的大小
    std::vector<std::size_t> size_of_every_demension = std::vector<std::size_t>();
    // 数组类型
    if (wrds[ci].word_type == mysh::WORD_TYPE::ARRAY)
    {
        // 至少有一维
        dimension++;
        // 获取数组内部类型
        ci++;
        // 当前应该是左尖括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LES)
        {
            // 尖括号计数
            std::int32_t angle_brackets_count = 0;
            angle_brackets_count++;
            ci++;
            while (ci < wn && angle_brackets_count > 0)
            {
                if (wrds[ci].word_type == mysh::WORD_TYPE::ARRAY)
                {
                    dimension++;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::LES)
                {
                    angle_brackets_count++;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::GTR)
                {
                    angle_brackets_count--;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::BOOL)
                {
                    data_type = mysh::WORD_TYPE::BOOL_VALUE;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::INT)
                {
                    data_type = mysh::WORD_TYPE::INTEGER;
                }
                else if (wrds[ci].word_type == mysh::WORD_TYPE::STRING)
                {
                    data_type = mysh::WORD_TYPE::STRING_VALUE;
                }
                else
                {
                    throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句缺少正确的返回数据类型关键字\033[0m");
                }
                ci++;
            }
            if (data_type == mysh::WORD_TYPE::ILLEGAL_WORD)
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句缺少正确的返回数据类型关键字\033[0m");
            }
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
            {
                ci++;
                while (ci < wn && wrds[ci].word_type != mysh::WORD_TYPE::RIGHT_PARENTHESES)
                {
                    if (wrds[ci].word_type == mysh::WORD_TYPE::INTEGER)
                    {
                        try
                        {
                            size_of_every_demension.push_back(std::stoi(wrds[ci].name));
                        }
                        catch(const std::exception& e)
                        {
                            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句指名各维度大小的数据有误: " + e.what() + "\033[0m");
                        }
                    }
                    else if (wrds[ci].word_type == mysh::WORD_TYPE::COMMA)
                    {
                        ci++;
                        continue;
                    }
                    else
                    {
                        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句指名各维度大小的单词类型有误\033[0m");
                    }
                    ci++;
                }
                if (size_of_every_demension.size() != dimension)
                {
                    throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句指名各维度大小的数据量与维度不匹配\033[0m");
                }
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句中的数组返回数据类型标记缺少指定各维度大小的小括号\033[0m");
            }
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句中的数组返回数据类型标记缺少尖括号\033[0m");
        }
    }
    // bool类型
    else if (wrds[ci].word_type == mysh::WORD_TYPE::BOOL)
    {
        data_type = mysh::WORD_TYPE::BOOL_VALUE;
    }
    // int类型
    else if (wrds[ci].word_type == mysh::WORD_TYPE::INT)
    {
        data_type = mysh::WORD_TYPE::INTEGER;
    }
    // string类型
    else if (wrds[ci].word_type == mysh::WORD_TYPE::STRING)
    {
        data_type = mysh::WORD_TYPE::STRING_VALUE;
    }
    else if (wrds[ci].word_type == mysh::WORD_TYPE::VOID)
    {
        data_type = mysh::WORD_TYPE::VOID;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句缺少正确的返回数据类型关键字\033[0m");
    }
    // 应以分号结尾
    ci++;
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m运行语句未以分号结尾\033[0m");
    }
    return std::make_shared<mysh::RunStatementNode>(params, data_type, dimension, size_of_every_demension, command);
}

// 分析输入输出语句
std::shared_ptr<mysh::InputOutputStatementNode> mysh::MyShellParser::parseInputOutputStatement()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 指名是输入语句还是输出语句
    bool input_output = wrds[ci].word_type == mysh::WORD_TYPE::INPUT;
    ci++;
    if (ci < wn)
    {
        // 输入语句需要两个大于号
        if (input_output)
        {
            if (wrds[ci].word_type == mysh::WORD_TYPE::GTR)
            {
                ci++;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输入语句在input之后需要两个大于号\033[0m");
            }
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::GTR)
            {
                ci++;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输入语句在input之后需要两个大于号\033[0m");
            }
        }
        // 输出语句需要两个小于号
        else
        {
            if (wrds[ci].word_type == mysh::WORD_TYPE::LES)
            {
                ci++;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输出语句在output之后需要两个小于号\033[0m");
            }
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LES)
            {
                ci++;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输出语句在output之后需要两个小于号\033[0m");
            }
        }
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输入或输出语句不完整\033[0m");
    }
    // 获取用于输入输出的标识符
    std::string identifier_to_io = std::string();
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        identifier_to_io = wrds[ci].name;
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输入或输出语句未指定标识符\033[0m");
    }
    // 应以分号结尾
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m输入或输出语句未以分号结尾\033[0m");
    }
    return std::make_shared<mysh::InputOutputStatementNode>(input_output, identifier_to_io);
}

// 分析左值节点
std::shared_ptr<mysh::LeftValueNode> mysh::MyShellParser::parseLeftValueNode()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 标识符
    std::string identifier = std::string();
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        identifier = wrds[ci].name;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m左值需要以标识符开头\033[0m");
    }
    ci++;
    std::vector<mysh::Word> indexes = std::vector<mysh::Word>();
    // 有左中括号，表明有索引
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_SQUARE_BRACKETS)
    {
        // 检测是否分析完毕
        bool if_finish = false;
        ci++;
        while (ci < wn && !if_finish)
        {
            // 标识符或者整数可作为索引
            if (wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER || wrds[ci].word_type == mysh::WORD_TYPE::INTEGER)
            {
                indexes.push_back(wrds[ci]);
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m左值索引序列中使用了不合理的单词\033[0m");
            }
            ci++;
            // 遇到右中括号，暂时认为分析完毕
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_SQUARE_BRACKETS)
            {
                if_finish = true;
            }
            else
            {
                throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m左值索引序列不符合规范\033[0m");
            }
            ci++;
            // 遇到左中括号，说明还有索引序列，继续分析
            if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_SQUARE_BRACKETS)
            {
                if_finish = false;
                ci++;
            }
        }
    }
    return std::make_shared<mysh::LeftValueNode>(identifier, indexes);
}

// 分析数值节点
std::shared_ptr<mysh::DataNode> mysh::MyShellParser::parseDataNode()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 数值类型
    mysh::WORD_TYPE data_type = mysh::WORD_TYPE::ILLEGAL_WORD;
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::INTEGER)
    {
        data_type = mysh::WORD_TYPE::INTEGER;
    }
    else if (ci < wn && (wrds[ci].word_type == mysh::WORD_TYPE::TRUE || wrds[ci].word_type == mysh::WORD_TYPE::FALSE))
    {
        data_type = mysh::WORD_TYPE::BOOL_VALUE;
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE)
    {
        data_type = mysh::WORD_TYPE::STRING_VALUE;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m赋值语句赋值号右边的数值型单词类型不正确\033[0m");
    }
    // 数值
    std::string value = std::string();
    value = wrds[ci].name;
    ci++;
    return std::make_shared<mysh::DataNode>(data_type, value);
}

// 分析赋值语句
std::shared_ptr<mysh::AssignStatementNode> mysh::MyShellParser::parseAssignStatement()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 左值节点
    std::shared_ptr<mysh::LeftValueNode> left_value = mysh::MyShellParser::parseLeftValueNode();
    // 左值节点之后需要有赋值符号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::ASSIGN)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m出现单独的左值，请检查是否需要对此左值进行赋值\033[0m");
    }
    std::shared_ptr<mysh::GrammarNode> expression = nullptr;
    // 运行语句
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RUN)
    {
        ci++;
        expression = mysh::MyShellParser::parseRunStatement();
    }
    // 数值节点
    else if (
        ci < wn && (
        wrds[ci].word_type == mysh::WORD_TYPE::INTEGER || 
        wrds[ci].word_type == mysh::WORD_TYPE::TRUE ||
        wrds[ci].word_type == mysh::WORD_TYPE::FALSE || 
        wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE))
    {
        expression = mysh::MyShellParser::parseDataNode();
        // 应以分号结尾
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m赋值语句未以分号结尾\033[0m");
        }
    }
    // 另一个左值
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        expression = mysh::MyShellParser::parseLeftValueNode();
        // 应以分号结尾
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m赋值语句未以分号结尾\033[0m");
        }
    }
    // 函数调用
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::CALL)
    {
        // to do
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m请检查赋值号之后的语句是否是数值或可返回语句\033[0m");
    }
    return std::make_shared<mysh::AssignStatementNode>(left_value, expression);
}

// 分析条件节点
std::shared_ptr<mysh::GrammarNode> mysh::MyShellParser::parseConditionNode()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 条件可以是左值、右值、运行语句、函数调用
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        return mysh::MyShellParser::parseLeftValueNode();
    }
    else if (
        ci < wn && (
        wrds[ci].word_type == mysh::WORD_TYPE::INTEGER || 
        wrds[ci].word_type == mysh::WORD_TYPE::TRUE || 
        wrds[ci].word_type == mysh::WORD_TYPE::FALSE || 
        wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE))
    {
        return mysh::MyShellParser::parseDataNode();
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RUN)
    {
        ci++;
        return mysh::MyShellParser::parseRunStatement();
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::CALL)
    {
        // to do
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m当前分析的语法节点不可用作条件\033[0m");
    }
    return std::shared_ptr<mysh::GrammarNode>();
}

// 分析if-else语句块
std::shared_ptr<mysh::IfElseStatementBlock> mysh::MyShellParser::parseIfElseStatementBlock()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 所有分析出来的条件
    std::vector<std::shared_ptr<mysh::GrammarNode>> conditions = std::vector<std::shared_ptr<mysh::GrammarNode>>();
    // 每个分支语句块内的语句
    std::vector<std::vector<std::shared_ptr<mysh::GrammarNode>>> statements = std::vector<std::vector<std::shared_ptr<mysh::GrammarNode>>>();
    // if之后要有左小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mif之后缺少左小括号\033[0m");
    }
    // 分析条件
    conditions.push_back(mysh::MyShellParser::parseConditionNode());
    // 条件之后应有右小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m条件之后缺少右小括号\033[0m");
    }
    // 右小括号之后是左大括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_BRACE)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m条件之后缺少左大括号\033[0m");
    }
    // 左大括号之后是语句块内的语句
    statements.push_back(mysh::MyShellParser::parseStatementsInBlock());
    // 接下来若是elif，则表明有其他分支，继续分析
    while (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::ELIF)
    {
        ci++;
        // elif之后要有左小括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34melif之后缺少左小括号\033[0m");
        }
        // 分析条件
        conditions.push_back(mysh::MyShellParser::parseConditionNode());
        // 条件之后应有右小括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_PARENTHESES)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m条件之后缺少右小括号\033[0m");
        }
        // 右小括号之后是左大括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_BRACE)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34m条件之后缺少左大括号\033[0m");
        }
        // 左大括号之后是语句块内的语句
        statements.push_back(mysh::MyShellParser::parseStatementsInBlock());
    }
    // 接下来若遇到else，则分析最后一个分支
    if (ci < wn && wrds[ci].word_type == ELSE)
    {
        ci++;
        // else条件为真
        conditions.push_back(std::make_shared<mysh::DataNode>(mysh::WORD_TYPE::BOOL_VALUE, "true"));
        // else之后是左大括号
        if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_BRACE)
        {
            ci++;
        }
        else
        {
            throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34melse之后缺少左大括号\033[0m");
        }
        // 左大括号之后是语句块内的语句
        statements.push_back(mysh::MyShellParser::parseStatementsInBlock());
    }
    // 应以分号结尾
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mif-else语句块未以分号结尾\033[0m");
    }
    return std::make_shared<mysh::IfElseStatementBlock>(conditions, statements);
}

// 分析for语句块
std::shared_ptr<mysh::GrammarNode> mysh::MyShellParser::parseForStatementBlock()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // for之后要有左小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor之后缺少左小括号\033[0m");
    }
    // 左小括号之后是数值或左值节点
    std::shared_ptr<mysh::GrammarNode> node_before = nullptr;
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        node_before = mysh::MyShellParser::parseLeftValueNode();
    }
    else if (
        ci < wn && (
        wrds[ci].word_type == mysh::WORD_TYPE::INTEGER || 
        wrds[ci].word_type == mysh::WORD_TYPE::TRUE || 
        wrds[ci].word_type == mysh::WORD_TYPE::FALSE || 
        wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE))
    {
        node_before = mysh::MyShellParser::parseDataNode();
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块左小括号之后应该是左值或者数值节点\033[0m");
    }
    // 之后确定for语句块类型
    mysh::GRAMMAR_NODE_TYPE for_statement_block_type = mysh::GRAMMAR_NODE_TYPE::ILLEGAL_GRAMMAR;
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IN)
    {
        for_statement_block_type = mysh::GRAMMAR_NODE_TYPE::FOR_IN_BLOCK;
        ci++;
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::TO)
    {
        for_statement_block_type = mysh::GRAMMAR_NODE_TYPE::FOR_TO_BLOCK;
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块类型无法确定\033[0m");
    }
    // 之后是数值、左值、运行语句、调用语句节点
    std::shared_ptr<mysh::GrammarNode> node_after = nullptr;
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
    {
        node_after = mysh::MyShellParser::parseLeftValueNode();
    }
    else if (
        ci < wn && (
        wrds[ci].word_type == mysh::WORD_TYPE::INTEGER || 
        wrds[ci].word_type == mysh::WORD_TYPE::TRUE || 
        wrds[ci].word_type == mysh::WORD_TYPE::FALSE || 
        wrds[ci].word_type == mysh::WORD_TYPE::STRING_VALUE))
    {
        node_after = mysh::MyShellParser::parseDataNode();
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RUN)
    {
        ci++;
        node_after = mysh::MyShellParser::parseRunStatement();
    }
    else if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::CALL)
    {
        // to do
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块缺少范围限定节点\033[0m");
    }
    // 之后是右小括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_PARENTHESES)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块缺少右小括号\033[0m");
    }
    // 之后是左大括号
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::LEFT_BRACE)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块缺少左大括号\033[0m");
    }
    // 之后是块内语句
    std::vector<std::shared_ptr<mysh::GrammarNode>> statements = mysh::MyShellParser::parseStatementsInBlock();
    // 应以分号结尾
    if (ci < wn && wrds[ci].word_type == mysh::WORD_TYPE::SEMICOLON)
    {
        ci++;
    }
    else
    {
        throw mysh::GrammarException(std::string("\033[33m单词序号: \033[31m") + std::to_string(ci) + "\n  \033[34mfor语句块未以分号结尾\033[0m");
    }
    // 根据for语句块类型返回节点
    if (for_statement_block_type == mysh::GRAMMAR_NODE_TYPE::FOR_IN_BLOCK)
    {
        return std::make_shared<mysh::ForInStatementBlock>(node_before, node_after, statements);
    }
    return std::make_shared<mysh::ForToStatementBlock>(node_before, node_after, statements);
}

// 分析语句块内的语句
std::vector<std::shared_ptr<mysh::GrammarNode>> mysh::MyShellParser::parseStatementsInBlock()
{
    // cur_index别名
    std::int32_t& ci = mysh::MyShellParser::cur_index;
    // word_num别名
    std::size_t& wn = mysh::MyShellParser::word_num;
    // words别名
    std::vector<mysh::Word>& wrds = mysh::MyShellParser::words;
    // 答案数组
    std::vector<std::shared_ptr<mysh::GrammarNode>> ans = std::vector<std::shared_ptr<mysh::GrammarNode>>();
    // 开始分析
    while (ci < wn)
    {
        // 跳过注释
        if (wrds[ci].word_type == mysh::WORD_TYPE::EXPLANATORY_NOTE)
        {
            ci++;
        }
        // 遇到右大括号说明当前语句块分析完毕，退出循环
        else if (wrds[ci].word_type == mysh::WORD_TYPE::RIGHT_BRACE)
        {
            ci++;
            break;
        }
        // 声明语句
        else if (wrds[ci].word_type == mysh::WORD_TYPE::DECLARE)
        {
            std::cerr << "\033[32m" << "开始分析声明语句" << "\033[0m" << std::endl;
            ci++;
            ans.push_back(mysh::MyShellParser::parseDeclareStatement());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
        // 运行语句
        else if (wrds[ci].word_type == mysh::WORD_TYPE::RUN)
        {
            std::cerr << "\033[32m" << "开始分析运行语句" << "\033[0m" << std::endl;
            ci++;
            ans.push_back(mysh::MyShellParser::parseRunStatement());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
        // 输入或输出语句
        else if (wrds[ci].word_type == mysh::WORD_TYPE::INPUT || wrds[ci].word_type == mysh::WORD_TYPE::OUTPUT)
        {
            std::cerr << "\033[32m" << "开始分析输入输出语句" << "\033[0m" << std::endl;
            ans.push_back(mysh::MyShellParser::parseInputOutputStatement());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
        // 赋值语句
        else if (wrds[ci].word_type == mysh::WORD_TYPE::IDENTIFIER)
        {
            std::cerr << "\033[32m" << "开始分析赋值语句" << "\033[0m" << std::endl;
            ans.push_back(mysh::MyShellParser::parseAssignStatement());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
        // if-else语句块
        else if (wrds[ci].word_type == mysh::WORD_TYPE::IF)
        {
            std::cerr << "\033[32m" << "开始分析if-else语句块" << "\033[0m" << std::endl;
            ci++;
            ans.push_back(mysh::MyShellParser::parseIfElseStatementBlock());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
        // for语句块
        else if (wrds[ci].word_type == mysh::WORD_TYPE::FOR)
        {
            std::cerr << "\033[32m" << "开始分析for语句块" << "\033[0m" << std::endl;
            ci++;
            ans.push_back(mysh::MyShellParser::parseForStatementBlock());
            std::cerr << "\033[32m" << "分析完毕" << "\033[0m" << std::endl;
        }
    }
    return ans;
}

// 语法分析主入口
std::vector<std::shared_ptr<mysh::GrammarNode>> mysh::MyShellParser::parse(const std::vector<mysh::Word>& words)
{
    // 初始化
    // 要分析的单词序列
    mysh::MyShellParser::words = words;
    // 所需分析的单词序列中单词的数量
    mysh::MyShellParser::word_num = mysh::MyShellParser::words.size();
    // 当前分析的单词的索引
    mysh::MyShellParser::cur_index = 0;
    // 开始分析
    return mysh::MyShellParser::parseStatementsInBlock();
}

// 打印缩进
std::ostream& mysh::MyShellParser::printIndentation(const std::int32_t& indentation)
{
    for (std::int32_t i = 0; i < indentation * 2; i++)
    {
        std::cerr << ' ';
    }
    return (std::cerr << std::flush);
}

// 打印声明语句信息
void mysh::MyShellParser::printDeclareStatementMessage(const std::shared_ptr<mysh::DeclareStatementNode>& declare_statement_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "声明语句:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "维度:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << declare_statement_node->dimension << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "数据类型:" << std::endl;
    if (declare_statement_node->data_type == mysh::WORD_TYPE::INTEGER)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "整型" << (declare_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    else if (declare_statement_node->data_type == mysh::WORD_TYPE::BOOL_VALUE)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "布尔" << (declare_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    else if (declare_statement_node->data_type == mysh::WORD_TYPE::STRING_VALUE)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "字符串" << (declare_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    if (declare_statement_node->dimension > 0)
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "各维度大小: " << std::endl;
        std::size_t n = declare_statement_node->size_of_every_demension.size();
        for (std::int32_t i = 0; i < n; i++)
        {
            mysh::MyShellParser::printIndentation(indentation + 2) << "\033[35m" << "第" << i + 1 << "维: " << "\033[32m" << declare_statement_node->size_of_every_demension[i] << std::endl;
        }
    }
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "声明的标识符:" << std::endl;
    for (auto name : declare_statement_node->identifiers)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << name << std::endl;
    }
    std::cerr << "\033[0m" << std::flush;
}

// 打印运行语句信息
void mysh::MyShellParser::printRunStatementMessage(const std::shared_ptr<mysh::RunStatementNode>& run_statement_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "运行语句:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "要执行的命令:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << run_statement_node->command << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "参数列表:" << std::endl;
    for (auto word : run_statement_node->param_list)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << word.name << std::endl;
    }
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "返回数据类型:" << std::endl;
    if (run_statement_node->data_type == mysh::WORD_TYPE::INTEGER)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "整型" << (run_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    else if (run_statement_node->data_type == mysh::WORD_TYPE::BOOL_VALUE)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "布尔" << (run_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    else if (run_statement_node->data_type == mysh::WORD_TYPE::STRING_VALUE)
    {
        mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << "字符串" << (run_statement_node->dimension > 0 ? "数组" : "") << std::endl;
    }
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "返回数据维度: " << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << run_statement_node->dimension << std::endl;
    if (run_statement_node->dimension > 0)
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "返回数据各维度大小: " << std::endl;
        std::size_t n = run_statement_node->size_of_every_demension.size();
        for (std::int32_t i = 0; i < n; i++)
        {
            mysh::MyShellParser::printIndentation(indentation + 2) << "\033[35m" << "第" << i + 1 << "维: " << "\033[32m" << run_statement_node->size_of_every_demension[i] << std::endl;
        }
    }
    std::cerr << "\033[0m" << std::flush;
}

// 打印输入输出语句信息
void mysh::MyShellParser::printInputOutputStatementMessage(const std::shared_ptr<mysh::InputOutputStatementNode>& io_statement_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << (io_statement_node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::INPUT_STATEMENT ? "输入" : "输出") << "语句:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "用于" << (io_statement_node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::INPUT_STATEMENT ? "输入" : "输出") << "的标识符: " << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << io_statement_node->identifier << "\033[0m" << std::endl;
}

// 打印左值节点
void mysh::MyShellParser::printLeftValueNode(const std::shared_ptr<mysh::LeftValueNode>& left_value_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "左值节点:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "标识符:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << left_value_node->identifier << std::endl;
    if (!left_value_node->index.empty())
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "各维度索引:" << std::endl;
        std::size_t n = left_value_node->index.size();
        for (std::int32_t i = 0; i < n; i++)
        {
            mysh::MyShellParser::printIndentation(indentation + 2) << "\033[35m" << "第" << i + 1 << "个索引: " << "\033[32m" << left_value_node->index[i].name << std::endl;
        }
    }
    std::cerr << "\033[0m" << std::flush;
}

// 打印数值节点
void mysh::MyShellParser::printDataNode(const std::shared_ptr<mysh::DataNode>& data_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "数值节点:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "数值:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 2) << "\033[32m" << data_node->value << "\033[0m" << std::endl;
}

// 打印赋值语句信息
void mysh::MyShellParser::printAssignStatementMessage(const std::shared_ptr<mysh::AssignStatementNode>& assign_statement_node, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "赋值语句:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "等号左边的变量:" << std::endl;
    mysh::MyShellParser::printLeftValueNode(assign_statement_node->left_value, indentation + 2);
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "等号右边的表达式:" << std::endl;
    if (assign_statement_node->expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
    {
        mysh::MyShellParser::printRunStatementMessage(std::static_pointer_cast<mysh::RunStatementNode>(assign_statement_node->expression), indentation + 2);
    }
    else if (assign_statement_node->expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(assign_statement_node->expression), indentation + 2);
    }
    else if (assign_statement_node->expression->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(assign_statement_node->expression), indentation + 2);
    }
    else
    {
        // to do
    }
}

// 打印if-else语句块信息
void mysh::MyShellParser::printIfElseStatementBlockMessage(const std::shared_ptr<mysh::IfElseStatementBlock>& if_else_statement_block, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "if-else语句块:" << std::endl;
    if (!if_else_statement_block->conditions.empty())
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "各个分支的条件语句:" << std::endl;
        std::size_t n = if_else_statement_block->conditions.size();
        for (std::int32_t i = 0; i < n; i++)
        {
            mysh::MyShellParser::printIndentation(indentation + 2) << "\033[35m" << "第" << i + 1 << "个分支的条件:" << std::endl;
            mysh::GRAMMAR_NODE_TYPE cur_grammar_node_type = if_else_statement_block->conditions[i]->grammar_node_type;
            if (cur_grammar_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
            {
                mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(if_else_statement_block->conditions[i]), indentation + 3);
            }
            else if (cur_grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
            {
                mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(if_else_statement_block->conditions[i]), indentation + 3);
            }
            else if (cur_grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
            {
                mysh::MyShellParser::printRunStatementMessage(std::static_pointer_cast<mysh::RunStatementNode>(if_else_statement_block->conditions[i]), indentation + 3);
            }
            else
            {
                // to do
            }
        }
    }
    if (!if_else_statement_block->statements.empty())
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "各个分支的块内语句:" << std::endl;
        std::size_t n = if_else_statement_block->statements.size();
        for (std::int32_t i = 0; i < n; i++)
        {
            mysh::MyShellParser::printIndentation(indentation + 2) << "\033[35m" << "第" << i + 1 << "个分支的块内语句:" << std::endl;
            mysh::MyShellParser::printParseMessage(if_else_statement_block->statements[i], indentation + 3);
        }
    }
}

// 打印for-in语句块信息
void mysh::MyShellParser::printForInStatementBlockMessage(const std::shared_ptr<mysh::ForInStatementBlock>& for_in_statement_block, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "for-in语句块:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "in前节点:" << std::endl;
    mysh::GRAMMAR_NODE_TYPE before_in_node_type = for_in_statement_block->node_before_in->grammar_node_type;
    if (before_in_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(for_in_statement_block->node_before_in), indentation + 2);
    }
    else if (before_in_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(for_in_statement_block->node_before_in), indentation + 2);
    }
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "in后节点:" << std::endl;
    mysh::GRAMMAR_NODE_TYPE after_in_node_type = for_in_statement_block->node_after_in->grammar_node_type;
    if (after_in_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(for_in_statement_block->node_after_in), indentation + 2);
    }
    else if (after_in_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(for_in_statement_block->node_after_in), indentation + 2);
    }
    else if (after_in_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
    {
        mysh::MyShellParser::printRunStatementMessage(std::static_pointer_cast<mysh::RunStatementNode>(for_in_statement_block->node_after_in), indentation + 2);
    }
    else
    {
        // to do
    }
    if (!for_in_statement_block->statements.empty())
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "块内语句:" << std::endl;
        mysh::MyShellParser::printParseMessage(for_in_statement_block->statements, indentation + 2);
    }
}

// 打印for-to语句块信息
void mysh::MyShellParser::printForToStatementBlockMessage(const std::shared_ptr<mysh::ForToStatementBlock>& for_to_statement_block, std::int32_t indentation)
{
    mysh::MyShellParser::printIndentation(indentation) << "\033[33m" << "for-to语句块:" << std::endl;
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "to前节点:" << std::endl;
    mysh::GRAMMAR_NODE_TYPE before_to_node_type = for_to_statement_block->node_before_to->grammar_node_type;
    if (before_to_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(for_to_statement_block->node_before_to), indentation + 2);
    }
    else if (before_to_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(for_to_statement_block->node_before_to), indentation + 2);
    }
    mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "to后节点:" << std::endl;
    mysh::GRAMMAR_NODE_TYPE after_to_node_type = for_to_statement_block->node_after_to->grammar_node_type;
    if (after_to_node_type == mysh::GRAMMAR_NODE_TYPE::LEFT_VALUE_NODE)
    {
        mysh::MyShellParser::printLeftValueNode(std::static_pointer_cast<mysh::LeftValueNode>(for_to_statement_block->node_after_to), indentation + 2);
    }
    else if (after_to_node_type == mysh::GRAMMAR_NODE_TYPE::DATA_NODE)
    {
        mysh::MyShellParser::printDataNode(std::static_pointer_cast<mysh::DataNode>(for_to_statement_block->node_after_to), indentation + 2);
    }
    else if (after_to_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
    {
        mysh::MyShellParser::printRunStatementMessage(std::static_pointer_cast<mysh::RunStatementNode>(for_to_statement_block->node_after_to), indentation + 2);
    }
    else
    {
        // to do
    }
    if (!for_to_statement_block->statements.empty())
    {
        mysh::MyShellParser::printIndentation(indentation + 1) << "\033[34m" << "块内语句:" << std::endl;
        mysh::MyShellParser::printParseMessage(for_to_statement_block->statements, indentation + 2);
    }
}

// 打印语法分析所有信息
void mysh::MyShellParser::printParseMessage(const std::vector<std::shared_ptr<mysh::GrammarNode>>& all_grammar_nodes, std::int32_t indentation)
{
    for (const std::shared_ptr<mysh::GrammarNode>& node : all_grammar_nodes)
    {
        // 声明语句
        if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::DECLARE_STATEMENT)
        {
            mysh::MyShellParser::printDeclareStatementMessage(std::static_pointer_cast<mysh::DeclareStatementNode>(node), indentation);
        }
        // 运行语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::RUN_STATEMENT)
        {
            mysh::MyShellParser::printRunStatementMessage(std::static_pointer_cast<mysh::RunStatementNode>(node), indentation);
        }
        // 输入或输出语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::INPUT_STATEMENT || node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::OUTPUT_STATEMENT)
        {
            mysh::MyShellParser::printInputOutputStatementMessage(std::static_pointer_cast<mysh::InputOutputStatementNode>(node), indentation);
        }
        // 赋值语句
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::ASSIGN_STATEMENT)
        {
            mysh::MyShellParser::printAssignStatementMessage(std::static_pointer_cast<mysh::AssignStatementNode>(node), indentation);
        }
        // if-else语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::IF_ELSE_BLOCK)
        {
            mysh::MyShellParser::printIfElseStatementBlockMessage(std::static_pointer_cast<mysh::IfElseStatementBlock>(node), indentation);
        }
        // for-in语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::FOR_IN_BLOCK)
        {
            mysh::MyShellParser::printForInStatementBlockMessage(std::static_pointer_cast<mysh::ForInStatementBlock>(node), indentation);
        }
        // for-to语句块
        else if (node->grammar_node_type == mysh::GRAMMAR_NODE_TYPE::FOR_TO_BLOCK)
        {
            mysh::MyShellParser::printForToStatementBlockMessage(std::static_pointer_cast<mysh::ForToStatementBlock>(node), indentation);
        }
    }
}